/**
*@ClassName: AppLoginController
*@Description
*@Author maozhi
*@Date 2021/3/22
*@Time 14:59
*/

package com.universityJob.modules.app.controller;


import com.universityJob.common.utils.R;
import com.universityJob.common.validator.ValidatorUtils;
import com.universityJob.modules.app.form.LoginForm;
import com.universityJob.modules.app.service.UserService;
import com.universityJob.modules.app.utils.JwtUtils;
import com.universityJob.modules.app.utils.WXPublicUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 *
 */
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private WXPublicUtils wxPublicUtils;

    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestParam String code){
        if (code == null || code.isEmpty()){
            return R.error("CODE不能为空！");
        }
        String openId = wxPublicUtils.getOpenId(code);
        if ("".equals(openId) || openId.isEmpty()){
            return R.error("获取openid失败");
        }
        //生成token
        String token = jwtUtils.generateToken(openId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

}
